Skip to content

feat: Codex compactions become compaction spans (RIUS-1547) - #68

Merged
sudopower merged 2 commits into
mainfrom
factory/RIUS-1547
Oct 22, 2026
Merged

sudopower merged 2 commits into
mainfrom
factory/RIUS-1547

Conversation

@sudopower

@sudopower sudopower commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Ticket: RIUS-1547 (M3). Follows rius-coding-agents #67, the Claude Code half.

What changed

  • A Codex rollout compacted line becomes a compaction CHAIN span under the open turn: kind summarize, executor provider when the line's message is empty, else client (A42; see round 1 below), tokens.before, tokens.after, tokens.dropped, context_window, id = window_id, summary.bytes. Trigger is left unset (the plugin registers no Codex hooks).
  • The summary text is content: it goes through the capture gate, scrubbing and the cap, and is re-read from the rollout, never stored in the state file.
  • The first real model call after a compaction gets gen_ai.conversation.compacted = true, and no other. Codex's own estimate token_count after a local compaction (0 input, 0 output) is no call, takes no flag, and gives tokens.after.
  • Replay gives identical span ids; a rewound offset flags nothing and settles nothing; batched export equals a single pass; a compaction with no open turn (or inside a fork-copied turn) is dropped.
  • Docs: docs/codex.md (Compactions section), pointer in docs/how-it-works.md.

Real compaction: NOT checked

The format comes from Codex source (rollout_payload.rs, protocol.rs), not a captured file. No local Codex rollout holds a compaction, and a real one needs real credentials and the real ~/.codex, so it was not run. Per the ticket, the releaser keeps this change out of the plugin release PR until a real check is recorded. Known uncertainties, also in the docs: whether a summarizer call's own token_count can land after the compacted line is inferred.

Checks (head d0a2811)

  • /usr/bin/python3 -m pytest -q tests/test_codex_spans.py tests/test_codex_export.py tests/test_codex_compaction.py: 103 passed, 1 skipped
  • factory-heavy 900 /usr/bin/python3 -m pytest -q: 1854 passed, 9 skipped
  • Broke and restored: executor line, compact_next reset, before >= after (new boundary test added for it), _estimate().

Challenger waiver (P2, codex_spans.py:347)

Codex asked to keep every pending compaction until its size arrives. Waived on purpose: when a second compacted line comes before any token_count, the size after the first is never observed (the next count measures what the second left), so giving it to the first span would be a wrong number. The first keeps what it was sent with (no tokens.after), pinned by test_a_newer_compaction_takes_the_size_after_not_the_older_one.

Assumptions

None new. A37 (flag on the first LLM span only) applies here too.

Round 1 (reviewer: changes) - fixed in 41dedda

  • F1 (P2): executor no longer reads compaction_response_id, which Codex 0.162.1 sets for local and remote compactions alike. It is provider when the message is empty (remote v2), client when it holds summary text. Fixture: local line carries a response id, remote line has an empty message. Rule change recorded as assumption A42.
  • F2: a separate test gives the remote compaction an estimate after it; module docstring and docs corrected.
  • F3: tests for the three surviving mutants (estimate as next tokens.before, same-millisecond estimate, window learned after the line). All killed.
  • F4: a compaction right after an unsettled one has no tokens.before.
  • F5: noted in a code comment (rare "context window full" update). F6: not changed (two reads of a large line, no correctness issue).
  • Mutants re-run: executor flip, older-unsettled, estimate context_tokens, < vs <=, window fill: 5 of 5 killed. Full suite 1860 passed, 9 skipped.

…all is flagged (RIUS-1547)

A compacted rollout line is a compaction span (kind summarize, executor client or provider, sizes before and after, window id, summary bytes and gated text). The first real model call after it carries gen_ai.conversation.compacted. The format is from Codex source: no real compaction was captured.
@sudopower

Copy link
Copy Markdown
Member Author

factory/review: CHANGES (round 1) @ d0a2811

The structure is sound. The span is sent at the line and re-sent under the same id once the size after is known, the same pattern as PR 67. The summary text is re-read from the rollout behind the capture gate, so it never sits in the state file. The local estimate token_count is handled as a size, not a call. The "Real compaction: NOT checked" section is honest and matches what I found: no rollout on this machine holds a compacted line (233 MB of ~/.codex/sessions scanned by record type; the hits for "compacted" are in message text only).

Verified

  • Head pinned with gh pr view = d0a2811, 0 behind main.
  • /usr/bin/python3 -m pytest -q -p no:cacheprovider tests/test_codex_compaction.py tests/test_content_never_in_state.py: 49 passed.
  • factory-heavy 900 /usr/bin/python3 -m pytest -q -p no:cacheprovider: 1854 passed, 9 skipped. This runs on Python 3.9.6, so the floor is covered; no 3.10+ syntax in the diff. CI is green on every job (py3.9 floor through 3.13).
  • 37 mutants on codex_spans.py and codex_rollout.py: 32 killed, 2 equivalent (_positive accepting bool or 0: _int already drops bools, and 0 is falsy at every use), 3 survive (F3).
  • Premise, checked against the Codex source at tag rust-v0.162.1 (the installed version):
    • Field names: the CompactedItemWire strings in the installed binary include window_id, compaction_response_id and latest_token_usage_record. latest_token_usage_record is the same TokenUsageRecord that 0.162.1 writes as token_usage_record lines, so .usage.input_tokens is right.
    • Line order: session/mod.rs:4017-4110 replace_compacted_history writes compacted, then world_state, turn_context and a settings event_msg. After that, recompute_token_usage (mod.rs:4802-4840) sends a token_count with input=0, output=0, total=estimate. So the fixture's "compacted, then estimate" order and the _estimate() shape are right.
  • Waiver judged: sound. When a second compacted line comes before any count, the size after the first one is never observed, so leaving it without tokens.after is correct. A side effect is in F4.

Findings

F1 (P2, blocking): executor is provider for every Codex compaction. codex_spans.py:317-320.

  • 0.162.1 has two writers of the compacted line, and both set compaction_response_id:
    • Local summarization: core/src/compact.rs:401-414, message: summary_text, ... compaction_response_id: Some(compaction_response.response_id).
    • Remote v2 compaction: core/src/compact_remote_v2.rs:347-360, message: String::new(), ... compaction_response_id: Some(compaction_response_id).
  • So a local compaction (tasks/compact.rs, RemoteCompactionSupport::Unsupported) is labelled provider. That is the case the ticket's rule meant to call client ("provider when compaction_response_id is set, meaning remote compaction"); its premise doesn't hold in the version installed here. Your docs flag this as unconfirmed; the source confirms it.
  • Suggested fix: provider when the message is empty (remote v2 writes String::new()), client when it holds summary text. Update the fixture's local line to carry a response id too, so a test pins it. test_the_executor_is_the_provider_only_when_a_response_id_is_set pins the old rule, and the docs/codex.md caveat ("may carry one too") becomes a statement. Record the rule change as an assumption, since it departs from the ticket's wording.

F2 (P3): the fixture and docs say the remote compaction has no estimate after it. compact_remote_v2.rs:362 calls recompute_token_usage after the remote path as well. The code handles both cases, so this only affects the fixture's realism and the module docstring ("a remote one ... no estimate after it"). Consider giving the remote line an estimate in the fixture and keeping the no-estimate case as a separate test.

F3 (P3): test gaps, 3 surviving mutants.

  • Removing state["context_tokens"] = rec.get("context_tokens") in the estimate branch (codex_spans.py:269) passes all tests. That value is the next compaction's tokens.before when no real call comes between.
  • at_ns < pending["start_ns"] → <= in _settle_compaction passes all tests. A same-millisecond estimate is likely, because recompute_token_usage runs right after the line is written, and <= would skip it as tokens.after. The code is right; please add a same-millisecond test to pin it (the fixture puts the estimate 2 ms after the line, and the helper tests use whole seconds).
  • Removing the settle-time context_window fill (codex_spans.py:366-368) passes all tests.

F4 (P3, note): with two compacted lines and no count between, the newer one's tokens.before is the size before the first. state["context_tokens"] is not reset by a compaction. In test_a_newer_compaction_takes_the_size_after_not_the_older_one the "new" span gets before=9000, after=700, dropped=8300. In 0.162.1 this is reachable only when estimate_token_count_with_base_instructions returns None (so no estimate follows). Leaving tokens.before unset for a compaction that directly follows another unsettled one would be safer. Not blocking. I couldn't confirm whether the estimate can be None in practice (context_manager/history.rs:658).

F5 (P3): _estimate() also matches Codex's "context window full" count.

  • set_total_tokens_full (session/mod.rs:4885, called on ContextWindowExceeded, e.g. compact.rs:348) uses fill_to_context_window (protocol/src/protocol.rs:2345-2357). That writes last_token_usage = {total_tokens: window - cumulative total, everything else 0}.
  • When that delta is positive, _on_usage takes it as a local estimate: it becomes context_tokens, and so the next compaction's tokens.before, or the tokens.after of a pending one. The value is unrelated to the context size.
  • When the delta is 0, the normal path sets context_tokens = 0 and settles a pending compaction with nothing.
  • Usually harmless, because the delta is 0 in any long session and before then falls back to recorded_tokens. Worth a note in _estimate, or a check that the total stays below the last known context.

F6 (P3, note): compacted lines are large. They carry replacement_history, guardian_history and retained_context. With capture on, _recall parses the whole line again on each _emit_compaction, so twice per compaction in the hook path. Not a correctness issue.

Also noted, not a finding: tokens.before is a real prompt count (for a local compaction, the summarizer call's input_tokens, which includes the summarization prompt; that call's token_count lands before the line, compact.rs:~845), while tokens.after is Codex's estimate. So tokens.dropped mixes a measured and an estimated size. That matches the ticket; a code comment would help. The lines after compacted (world_state, turn_context, settings event) are safe: _on_turn_context only sets the model, and the others are dropped.

Sonnet challenger: it confirmed F1-F4, raised F5 (verified in the source above) and F6, and found no flag or settle bug.

Not verified

  • A real Codex compaction. None exists on this machine, and producing one needs a long real session with real credentials, which is outside the reviewer's remit. The releaser keeps this out of the plugin release until it is recorded, per the ticket.
  • Codex versions other than 0.162.1. Older rollouts may carry a numeric window_id (handled as no id) or no compaction_response_id.
  • The pre-v2 remote writer and the token-budget manual compaction path (compact_token_budget): not read. Message emptiness as the remote signal is confirmed for 0.162.1 v2 only. A local summary is never empty, since summary_text always starts with SUMMARY_PREFIX (compact.rs).
  • Backend ingestion of the re-sent span: I relied on the PR 67 precedent ("the backend keeps the latest copy"), not a live export.

… empty, and review fixes (RIUS-1547)

Both Codex writers set compaction_response_id, so it cannot tell a local
compaction from a remote one: the empty message can. A compaction that
follows an unsettled one has no size before of its own. Tests pin the
same-millisecond estimate, the estimate as the next size before, and the
window learned after the line.
@sudopower

Copy link
Copy Markdown
Member Author

factory/review: PASS (round 2) @ 41dedda

Re-review of the one commit on top of round 1 (d0a2811). It touches codex_spans.py, docs/codex.md, the synthetic fixture and the tests. The head is 1 behind main (the Cursor change in PR 69, which touches different files); GitHub reports it MERGEABLE/CLEAN.

Earlier findings

  • F1 (P2, blocking): fixed. The executor is client when the line carries summary text and provider when the message is empty (A42). The fixture's local line now carries a response id (resp_local_1), and the remote line has an empty message. Both a flip of the rule and a revert to the response-id rule fail the tests (4 and 2 tests fail).
  • F2: fixed. test_a_remote_compaction_is_followed_by_an_estimate_too covers the remote case with an estimate after it. The fixture keeps the no-estimate case, and the docs say a remote compaction is counted the same way.
  • F3: fixed. The estimate-branch context_tokens, < vs <= (with a new same-millisecond test) and the settle-time window fill each fail a test when broken.
  • F4: fixed. A compaction that follows an unsettled one gets no tokens.before. Ignoring older_unsettled fails a test. Dropping the recorded_tokens fallback in that case is also right: no count came between the two lines, so the newer line's usage record is the stale one too.
  • F5: fixed (documented in _on_usage).
  • F6: open, P3 note, not blocking (left as is, per the builder).

Verified

  • Head pinned with gh pr view = 41dedda.
  • /usr/bin/python3 -m pytest -q -p no:cacheprovider tests/test_codex_compaction.py tests/test_content_never_in_state.py tests/test_codex_spans.py tests/test_codex_export.py: 116 passed, 1 skipped.
  • factory-heavy 900 /usr/bin/python3 -m pytest -q -p no:cacheprovider: 1860 passed, 9 skipped (Python 3.9.6). CI: 21 checks pass.
  • 6 mutants on the changed code: all killed (executor flipped, executor back on the response id, older_unsettled ignored, estimate context_tokens removed, < → <=, window fill removed).
  • The message that decides the executor is read from the record in memory (codex_rollout._compacted) and is not stored in the state file. test_content_never_in_state.py passes.
  • Docs: the executor rule, the measured/estimated mix in tokens.dropped and the missing tokens.before after an unsettled compaction match the code. The caveat now says the rule holds for 0.162.1 only.

Findings

None new. F6 remains an open P3 note.

Not verified

  • A real Codex compaction (as in round 1): none exists on this machine. Per the ticket, the releaser keeps this out of the plugin release until one is recorded.
  • Codex writers before 0.162.1. A line with no message field would read as provider.
  • No second-model challenger on this round: the delta is small and each fix is pinned by a mutant above.

@sudopower
sudopower merged commit 43186a1 into main Oct 22, 2026
22 checks passed
@sudopower
sudopower deleted the factory/RIUS-1547 branch October 22, 2026 07:29
@sudopower sudopower mentioned this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant